package net.kesumu.yushabu.storage;

import java.util.ArrayList;

import net.kesumu.yushabu.storage.FriendContract.FriendEntry;
import net.kesumu.yushabuserver.entity.User;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class FriendDbOperator {

    YushabuDbHelper mDbHelper;
    
    public FriendDbOperator(Context context) {
        mDbHelper = new YushabuDbHelper(context);
    }
    
    public long insert(User friend) {
        // Gets the data repository in write mode
        SQLiteDatabase db = mDbHelper.getWritableDatabase();

        // Create a new map of values, where column names are the keys
        ContentValues values = new ContentValues();
        values.put(FriendEntry.COLUMN_NAME_FRIENDID, friend.getUserId());
        values.put(FriendEntry.COLUMN_NAME_FRIENDNAME, friend.getName());
        values.put(FriendEntry.COLUMN_NAME_FRIENDYUYUCHATID, friend.getYuyuChatId());

        // Insert the new row, returning the primary key value of the new row
        long newRowId;
        newRowId = db.insert(
                            FriendEntry.TABLE_NAME,
                            null,
                            values);
        return newRowId;
    }
    
    public User select(String friendId) {
        SQLiteDatabase db = mDbHelper.getReadableDatabase();

         // Define a projection that specifies which columns from the database
         // you will actually use after this query.
         String[] projection = {
             FriendEntry._ID,
             FriendEntry.COLUMN_NAME_FRIENDID,
             FriendEntry.COLUMN_NAME_FRIENDNAME,
             FriendEntry.COLUMN_NAME_FRIENDYUYUCHATID,
             FriendEntry.COLUMN_NAME_FRIENDPHOTOPATH
             };
         
         // Define 'where' part of query.
         String selection = FriendEntry.COLUMN_NAME_FRIENDID + " = ?";
         // Specify arguments in placeholder order.
         String[] selectionArgs = { friendId };
    
         // How you want the results sorted in the resulting Cursor
         String sortOrder =
                 FriendEntry.COLUMN_NAME_FRIENDNAME;
    
         Cursor cursor = db.query(
             FriendEntry.TABLE_NAME,  // The table to query
             projection,                               // The columns to return
             selection,                                // The columns for the WHERE clause
             selectionArgs,                            // The values for the WHERE clause
             null,                                     // don't group the rows
             null,                                     // don't filter by row groups
             sortOrder                                 // The sort order
             );
         if(cursor.getCount()>0) {
             cursor.moveToFirst();
             String itemFriendId = cursor.getString(
                 cursor.getColumnIndexOrThrow(FriendEntry.COLUMN_NAME_FRIENDID)
             );
             String itemFriendName = cursor.getString(
                     cursor.getColumnIndexOrThrow(FriendEntry.COLUMN_NAME_FRIENDNAME)
                 );
             String itemYuYuChatId = cursor.getString(
                     cursor.getColumnIndexOrThrow(FriendEntry.COLUMN_NAME_FRIENDYUYUCHATID)
                 );
             String itemFriendPhotoPath = cursor.getString(
                     cursor.getColumnIndexOrThrow(FriendEntry.COLUMN_NAME_FRIENDPHOTOPATH)
                 );
             User friend = new User(Integer.parseInt(itemFriendId),itemFriendName,itemYuYuChatId,"","","");
             friend.setPhotoPath(itemFriendPhotoPath);
             return friend;
         }
         return null;
    }
    
    public ArrayList<User> selectAll(){
        ArrayList<User> friends = new ArrayList<User>();
        SQLiteDatabase db = mDbHelper.getReadableDatabase();

        // Define a projection that specifies which columns from the database
        // you will actually use after this query.
        String[] projection = {
            FriendEntry._ID,
            FriendEntry.COLUMN_NAME_FRIENDID,
            FriendEntry.COLUMN_NAME_FRIENDNAME,
            FriendEntry.COLUMN_NAME_FRIENDYUYUCHATID,
            FriendEntry.COLUMN_NAME_FRIENDPHOTOPATH
            };
   
        // How you want the results sorted in the resulting Cursor
        String sortOrder =
                FriendEntry.COLUMN_NAME_FRIENDNAME;
   
        Cursor cursor = db.query(
            FriendEntry.TABLE_NAME,  // The table to query
            projection,                               // The columns to return
            null,                                // The columns for the WHERE clause
            null,                            // The values for the WHERE clause
            null,                                     // don't group the rows
            null,                                     // don't filter by row groups
            sortOrder                                 // The sort order
            );
        if(cursor.getCount()>0) {
            cursor.moveToFirst();
            do{
                String itemFriendId = cursor.getString(
                    cursor.getColumnIndexOrThrow(FriendEntry.COLUMN_NAME_FRIENDID)
                );
                String itemFriendName = cursor.getString(
                        cursor.getColumnIndexOrThrow(FriendEntry.COLUMN_NAME_FRIENDNAME)
                    );
                String itemFriendYuYuChatId = cursor.getString(
                        cursor.getColumnIndexOrThrow(FriendEntry.COLUMN_NAME_FRIENDYUYUCHATID)
                    );
                String itemFriendPhotoPath = cursor.getString(
                        cursor.getColumnIndexOrThrow(FriendEntry.COLUMN_NAME_FRIENDPHOTOPATH)
                    );
                User friend = new User();
                friend.setUserId(Integer.parseInt(itemFriendId));
                friend.setName(itemFriendName);
                friend.setYuyuChatId(itemFriendYuYuChatId);
                friend.setPhotoPath(itemFriendPhotoPath);
                friends.add(friend);
            }while(cursor.moveToNext());
        }
        return friends;
    }
    
    public int update(User friend) {
         SQLiteDatabase db = mDbHelper.getReadableDatabase();

         // New value for one column
         ContentValues values = new ContentValues();
         values.put(FriendEntry.COLUMN_NAME_FRIENDNAME, friend.getName());
         values.put(FriendEntry.COLUMN_NAME_FRIENDYUYUCHATID, friend.getYuyuChatId());
         values.put(FriendEntry.COLUMN_NAME_FRIENDPHOTOPATH, friend.getPhotoPath());
    
         // Which row to update, based on the ID
         String selection = FriendEntry.COLUMN_NAME_FRIENDID + " = ?";
         String[] selectionArgs = { String.valueOf(friend.getUserId()) };
    
         int count = db.update(
             FriendEntry.TABLE_NAME,
             values,
             selection,
             selectionArgs);
        return count;
    }

}
